(function() {
	var app = angular.module('WhiteListConfigApp', [ 'ui.bootstrap' ]);
	app
			.controller(
					'whiteListConfigController',
					function($http,$scope, $log, $uibModal) {
						$scope.insertComponent = {};
						var controller = this;
						controller.parameter = {};
						controller.queryParameter = "";
						this.whiteList = [];
						this.hostList = [];
						this.rulesList = [];
						this.queryCondition = {
							"hostId" : "",
							"rules" : "",
							"page" : 1,
							"size" : 5,
						};
						this.paginationInfo = {
							"maxSize" : 5,
							"totalPages" : 0,
							"totalElements" : 0
						};

						$scope.clickEvent = function() {
							controller.queryCondition.page = controller.queryCondition.forwordPage;
							loadData();
						}

						// 跳转页数，回车事件
						$scope.enterEvent = function(e) {
							var keycode = window.event ? e.keyCode : e.which;
							if (keycode == 13) {
								$scope.clickEvent();
							}
						}

						this.pageChanged = function() {
							loadData();
						}
						// 完成全选全部选事件
						$("#check_all").click(
								function() {
									$(".check_item").prop("checked",
											$(this).prop("checked"));
								});
						// 选中单个元素，如果单个元素选择满了，那么第一个主元素也被选中
						$(document)
								.on(
										"click",
										".check_item",
										function() {
											if ($(".check_item:checked").length == $(".check_item").length) {
												$("#check_all").prop("checked",
														true);
											}
											if ($(".check_item:checked").length == 0) {
												$("#check_all").prop("checked",
														false);
											}
										});
						
						//删除配置

						this.delConfig = function() {
							var basePath = $scope.getAppBasePath();
							var conditons =  new Array();
							var ips = "";
							$.each($(".check_item:checked"), function() {
								var id = "";   
								id = $(this).parents("tr").find(".whiteId").val();
								
								conditons.push(id);
								ips += $(this).parents("tr").find(".whiteIp").val()+"  ,  ";
							})
							ips = ips.substring(0,ips.length-1);
							// 如果没有选中的话
							if (conditons == "") {
								alert("请勾选需要删除的配置");
								return;
							} else {
							
								if (confirm("确认删除IP为["
										+ ips + "]的配置吗?")) {
									// 保存后台调用
									$
											.ajax({
												type : "POST",
												url : basePath
														+ "/data/delectWhiteConfig",
												dataType : "json",
												contentType : "application/json",
												data : JSON.stringify(conditons),
												success : function(data) {
													alert("删除成功");
													loadData();
												}
											});

								}
							}
						}
						
						
						// 新增配置
						this.insertConfig = function() {
							$scope.insertComponent = {};
							var modalInstanceAdd = $uibModal
									.open({
										backdrop : 'static',
										keyboard : false,
										scope : $scope,
										// animation :
										// $scope.animationsEnabled,// 打开时的动画开关
										templateUrl : 'js/controller/configuration.management/insertWhiteListConfig.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
										controller : 'insertWhiteListController',// 这是模态框的控制器,是用来控制模态框的
										ariaLabelledBy : 'modal-title',
										ariaDescribedBy : 'modal-body',
									// size : "lg",// 模态框的大小尺寸
									});

							modalInstanceAdd.result.then(function(result) {
								loadData();
							});
						}
						
						
						// 搜索事件
						this.search = function() {
							loadData();
						}

						// 重置事件
						this.reset = function() {
							this.queryCondition = {
									"hostId" : "",
									"rules" : "",
									"page" : 1,
									"size" : 5,
								};
						}
						
						// 程序初期化
						this.init = function() {
							loadData();
							loadHost();
							loadRules();
						}
						this.init();
						//加载loadHost
						function loadHost(){
							var basePath = $scope.getAppBasePath();
							// 后台调用
							 $.ajax({
							 type : "POST",
							 url : basePath + "/data/getWhiteListHostId",
							 dataType : "json",
							 contentType : "application/json",
							 //data : JSON.stringify(controller.parameter),
							 success : function(data) {
							 var jsonResult = angular.fromJson(data);
							 controller.hostList = jsonResult;
							 //$scope.controller.queryCondition.monitoringIndex=controller.indexList[0].name;
							 //手动刷新
							 $scope.$apply();  
							 }
							 });
						}
						
						//加载loadRules
						function loadRules(){
							var basePath = $scope.getAppBasePath();
							// 后台调用
							 $.ajax({
							 type : "POST",
							 url : basePath + "/data/getWhiteListRules",
							 dataType : "json",
							 contentType : "application/json",
							 //data : JSON.stringify(controller.parameter),
							 success : function(data) {
							 var jsonResult = angular.fromJson(data);
							 controller.rulesList = jsonResult;
							 //手动刷新
							 $scope.$apply();  
							 }
							 });
						}
						
						// 加载数据
						function loadData() {
							if ($(".check_item:checked").length == 0) {
								$("#check_all").prop("checked", false);
							}
							
							var basePath = $scope.getAppBasePath();
							// 参数获取
							controller.parameter.hostId = controller.queryCondition.hostId ||"";
							controller.parameter.rules = controller.queryCondition.rules ||"";
							controller.parameter.page = controller.queryCondition.page;
							controller.parameter.size = controller.queryCondition.size;
							
							// 后台调用
							 $.ajax({
							 type : "POST",
							 url : basePath + "/data/getWhiteListConfig",
							 dataType : "json",
							 contentType : "application/json",
							 data : JSON.stringify(controller.parameter),
							 success : function(data) {
							 var jsonResult = angular.fromJson(data);
							 controller.whiteList = jsonResult.list;
							 controller.paginationInfo.totalPages = jsonResult.pages;
							 controller.paginationInfo.totalElements = jsonResult.total;
							 //手动刷新
							 $scope.$apply();  
							 }
							 });

						}

					})
					.controller(
					'insertWhiteListController',
					function($scope, $uibModalInstance, $http, $log) {
						var basePath = $scope.getAppBasePath();
						var controller = this;
						this.hostList = [];
						this.rulesList = [];
						// 程序初期化
						this.init = function() {
							loadRules();
							loadHost();
						}

						this.init();
						// 加载数据
						//加载loadHost
						function loadHost(){
							var basePath = $scope.getAppBasePath();
							// 后台调用
							 $.ajax({
							 type : "POST",
							 url : basePath + "/data/getWhiteListHostId",
							 dataType : "json",
							 contentType : "application/json",
							 //data : JSON.stringify(controller.parameter),
							 success : function(data) {
							 var jsonResult = angular.fromJson(data);
							 $scope.ihostList = jsonResult;
							 for(var i=0;i<$scope.ihostList.length;i++){
								 $scope.ihostList[i].hostId= $scope.ihostList[i].hostId+"-"+ $scope.ihostList[i].ip
								}
							 
							 //$scope.controller.queryCondition.monitoringIndex=controller.indexList[0].name;
							 //手动刷新
							 $scope.$apply();  
							 }
							 });
						}
						
						//加载loadRules
						function loadRules(){
							var basePath = $scope.getAppBasePath();
							// 后台调用
							 $.ajax({
							 type : "POST",
							 url : basePath + "/data/getWhiteListRules",
							 dataType : "json",
							 contentType : "application/json",
							 //data : JSON.stringify(controller.parameter),
							 success : function(data) {
							 var jsonResult = angular.fromJson(data);
							 $scope.rulesList = jsonResult;
							 //手动刷新
							 $scope.$apply();  
							 }
							 });
						}
						
						
						$scope.save = function() {
							var basePath = $scope.getAppBasePath();
							// INSERT参数定义
							insertComponent = {
								"host" : "",
								"rules" : ""
							};
							// INSERT值获取
							insertComponent.host = $scope.$parent.insertComponent.host;
							insertComponent.rules = $scope.$parent.insertComponent.rules;
							if($scope.$parent.insertComponent.host == null && 
									$scope.$parent.insertComponent.rules == null){
								alert("请选择");
								return;
							}
							// 后台调用
							$.ajax({
								type : "POST",
								url : basePath + "/data/insertWhiteList",
								dataType : "json",
								contentType : "application/json",
								data : JSON.stringify(insertComponent),
								success : function(data) {
									$uibModalInstance.close();
									alert("插入成功");
								}
							});
							
						};
						$scope.cancel = function() {
							// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
							$uibModalInstance.dismiss('cancel');
						};
					})
			
})();